﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BusinessRefinery.Barcode;
using System.Drawing.Imaging;
using System.Drawing;
using System.Diagnostics;
using SuraQR.Config;
using System.Reflection;
using System.IO;
using SuraQR.Bean;

namespace SuraQR.Controller
{
    class QRCodeGenService
    {
        private QRCode _qr = new QRCode();
        private const int MAX_QR_SIZE = 900;
        private const string SAVE_PATH = "D:\\qr\\";
        private string[] _base64Img;
        private string img;

        public string[] Base64Img
        {
            get { return _base64Img; }
            set { _base64Img = value; }
        }
       

        public string[] genQR(string[] qrdata)
        {
            string _headQR ;
            _headQR = PropertiesConfig._Header +"#"+ PropertiesConfig._AppVersin + "#";
            _base64Img = new string[qrdata.Length];
            img = "";
            String _gid = Guid.NewGuid().ToString(); 
            for (int i = 0; i < qrdata.Length; i++)
            {
                string c = _headQR + (i + 1) + ":" + qrdata.Length + "#" + _gid + "#";
                _qr.Code = c + qrdata[i];
                _qr.Resolution = 100;
                _qr.Rotate = Rotate.Rotate90;
                _qr.DataMode = QRCodeDataMode.Auto;
                _qr.Format = ImageFormat.Png;
                _qr.Version = QRCodeVersion.V10;
                _qr.ECL = QRCodeECL.M;
                _qr.LeftMargin = 10;
                _qr.RightMargin = 10;
                _qr.TopMargin = 10;
                _qr.BottomMargin = 10;

                Debug.WriteLine(_qr.Code);

               // _qr.drawBarcode2ImageFile(SAVE_PATH + "QR" + i + ".png");
                _base64Img[i] = "<img src='" + PropertiesConfig._IMG_64
                    + Convert.ToBase64String(_qr.drawBarcode2ByteArray())
                    + "' />";
                img += _base64Img[i];
            }

            return _base64Img;
        }

        public void openDoc(String[] imageByTab,SOROR12011 fullData)
        {
            String contents = SuraQR.Properties.Resources.print2;
            string newdoc = contents.Replace("{{IMG1}}", imageByTab[0]);
            newdoc = newdoc.Replace("{{IMG4}}", imageByTab[1]);
            newdoc = newdoc.Replace("{{index2}}", "" + (imageByTab.Length ) );
            newdoc = newdoc.Replace("{{factoryname}}", "" + fullData.Addr.FactoryName);
            newdoc = newdoc.Replace("{{totalBth}}", "" + fullData.Tax.Data7 );

            int c = imageByTab.Length - 2 ;
            string tr = "";
            for (int i = 0; i < c; i++)
            {
                String ordertag = SuraQR.Properties.Resources.print2_2;
                tr += ordertag.Replace("{{index0}}", "" + (2+i));
                tr = tr.Replace("{{index1}}","" + (i+1) );
                tr = tr.Replace("{{IMG2}}", "" + imageByTab[(2 + i)]);
                if (fullData.OrderList.Length != 0)
                {
                    string info = "" + fullData.OrderList[i].AlcohorName + "<br/>แรงแอลกอฮอล์ " + fullData.OrderList[i].AlcohorDegree + " ดีกรี <br/> ขนาด " + fullData.OrderList[i].BottlePerLitter + "ลิตร";
                    tr = tr.Replace("{{orderInfo}}", info);
                }
                else
                {
                    tr = tr.Replace("{{orderInfo}}", "ไม่มี รายการ");
                }
            }
           
            newdoc = newdoc.Replace("{{orderQr}}",tr);

           String openPDFFile = @"temp_print_doc.html";
           string p = System.Reflection.Assembly.GetExecutingAssembly().Location;
           string exeDir = System.IO.Path.GetDirectoryName(p);
           Debug.WriteLine(exeDir);
           System.IO.File.WriteAllText(openPDFFile, newdoc);
           System.Diagnostics.Process.Start("iexplore.exe", exeDir + "\\" + openPDFFile); 

        }
        public string[] subQRCode(string rawdata, int pages = 0)
        {
            Debug.WriteLine("pages : " + pages);
            string[] tmp = new string[pages];
            int seek = 0;
            for (int i = 0; i < pages; i++)
            {
                if (i == pages - 1)
                {
                    tmp[i] = rawdata.Substring(seek, rawdata.Length - seek);
                    //Debug.WriteLine("last:" + tmp[i]);
                }
                else
                {
                    tmp[i] = rawdata.Substring(seek, MAX_QR_SIZE);
                    seek += MAX_QR_SIZE;
                }
            }

            return tmp;
        }

        public string[] subQRCode(string rawdata)
        {
            double d = Math.Ceiling(rawdata.Length / (MAX_QR_SIZE * 1D));
            Debug.WriteLine("page : " + d);
            string[] tmp = this.subQRCode(rawdata, (int)d);
            return tmp;
        }
              
    }
}
